var cf = require('../../lib/config.js');
var c = require('../../lib/common.js');
var login = require('../../lib/login.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    rd_seession: null,
    province: null,
    city: null,
    district: null,
    latitude: null,
    longitude: null,
    addressinfo: null,
    displayinfo: null,
    img: [],
    indexaddress: 0,
    region: ['北京', '北京市', '朝阳区'],
    array: ['手机', '微信', 'QQ号码'],
    index: 0,
    focus: false,
    focustitle: false,
    linktypefocus: false,
    arrayage: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17'],
    arraymonth: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
    arraytype: ['狗狗', '猫咪', '兔子', '鼠类', '其他'],
    indexage: 0,
    indexmonth: 0,
    indextype: 0,
    items: [
      { name: '1', value: '男', checked: 'true' },
      { name: '2', value: '女' },
    ],
    fang: [
      { name: '1', value: '自有房', checked: 'true' },
      { name: '2', value: '租房' },
      { name: '3', value: '其他' },
    ],
    hun: [
      { name: '1', value: '已婚', checked: 'true' },
      { name: '2', value: '未婚' },
    ],
    hai: [
      { name: '1', value: '有', checked: 'true' },
      { name: '2', value: '没有' },
    ],
    topname: '我想送养',
    prepage: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //1、获取当前位置坐标
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        //2、根据坐标获取当前位置名称，显示在顶部:腾讯地图逆地址解析
        var latitude = res.latitude
        var longitude = res.longitude
        that.setData({
          latitude: latitude,
          longitude: longitude,
        })
        wx.request({
          url: 'https://www.cwlyz.com/wx/Index/address', //仅为示例，并非真实的接口地址
          data: {
            lat: latitude,
            lng: longitude
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function (res) {
            that.setData({
              addressinfo: res.data.data.address,
              province: res.data.data.province,
              city: res.data.data.city,
              district: res.data.data.district,
            })

          }
        })

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  getUserInfo: function (e) {
    login.login(e, this);
  },
  onShow: function () {
    c.loginVer(this);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  bindinfo: function () {
    this.setData({
      focus: 1,
    })
  },
  bindButtonTaptitle: function () {
    this.setData({
      focustitle: 1,
    })
  },
  bindPickerChangeage: function (e) {
    this.setData({
      indexage: e.detail.value
    })
  },
  bindPickerChangemonth: function (e) {
    this.setData({
      indexmonth: e.detail.value
    })
  },
  bindPickerChangetype: function (e) {
    this.setData({
      indextype: e.detail.value
    })
  },
  bindRegionChange: function (e) { //城市选择
    console.log(e);
    var indexaddress = e.currentTarget.dataset.index;
    var region = e.detail.value;
    this.setData({
      region: region,
    })
  },

  imgtap: function () {

    var that = this;
    wx.chooseImage({
      success: (res) => {
        var i = 0; //第几个
        var img_num = res.tempFiles.length;
        this.uploadDIY(res.tempFilePaths, i, img_num);
      },
    });
  },

  uploadDIY(filePaths, i, img_num) {
    var img_num = img_num;
    wx.uploadFile({
      url: 'https://www.cwlyz.com/wx/Index/img_upinfo',
      filePath: filePaths[i],
      name: 'fileData',
      formData: {

      },
      success: (res) => {
        var imgold = this.data.img;
        var imgnew = res.data;
        var img = imgold.concat(imgnew);
        console.log(img);
        this.setData({ img: img });

      },
      complete: () => {
        i++;
        if (i == 6) {
          this.setData({ displayinfo: 'displayinfo' });
        }
        //递归调用uploadDIY函数
        if (i <= img_num - 1) {
          this.uploadDIY(filePaths, i, img_num);
        }
      },
    });
  },
  formSubmit: function (e) {
    wx.showLoading({
      title: '加载中',
    })
    var userinfo=wx.getStorageSync('userinfo');
    var id = userinfo.id;
    console.log('formSubmit');
    console.log(id);
    wx.request({
      url: 'https://www.cwlyz.com/wx/Index/myneedpet', //仅为示例，并非真实的接口地址
      data: {
        // age: e.detail.value.age,
        id: id,
        hun: e.detail.value.hun,
        latitude: e.detail.value.latitude,
        linknum: e.detail.value.linknum,
        linktype: e.detail.value.linktype,
        longitude: e.detail.value.longitude,
        fang: e.detail.value.fang,
        // month: e.detail.value.month,
        name: e.detail.value.name,
        sex: e.detail.value.sex,
        pettype: e.detail.value.pettype,
        hai: e.detail.value.hai,
        textarea: e.detail.value.textarea,
        title: e.detail.value.title,
        addressinfo: e.detail.value.addressinfo,
        province: e.detail.value.province,
        city: e.detail.value.city,
        district: e.detail.value.district,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        // console.log(res.data.res); return;
        if (res.data.res == 1) {
          wx.showToast({
            title: '上传成功',
            icon: 'success',
            duration: 2000
          })
          setTimeout(function () {
            wx.switchTab({
              url: '/pages/index/index',
            })

          }, 1000)
        } else {
          wx.showToast({
            title: '发布的文字不合法！',
            icon: 'none',
            image: '../../images/closered.png',
            duration: 2000
          })
        }
      }
    })
  },
  addressinfotab: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        that.setData({ addressinfo: res.address, latitude: res.latitude, longitude: res.longitude });
        //选择城市区域的时候 需要覆盖之前的 省市区 province city district
        wx.request({
          url: 'https://www.cwlyz.com/wx/Index/address', //仅为示例，并非真实的接口地址
          data: {
            lat: res.latitude,
            lng: res.longitude
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function (res) {
            that.setData({
              province: res.data.data.province,
              city: res.data.data.city,
              district: res.data.data.district,
            })

          }
        })
      }
    })
  }

})